    public function lists($ids = NULL)
    {
        $where = "where 1=1";
        // 新增：只显示本级及下级
        $admin_id = $this->request->get('admin_id');
        $filter_ids = [];
        if ($admin_id) {
            // 查找该管理员的ocation_id
            $admin = Db::name('admin')->where('id', $admin_id)->find();
            if ($admin && $admin['ocation_id']) {
                // 处理可能是逗号分隔的字符串
                $ocation_ids = is_string($admin['ocation_id']) ? explode(',', $admin['ocation_id']) : [$admin['ocation_id']];
                $filter_ids = [];
                foreach ($ocation_ids as $oid) {
                    if (!empty($oid)) {
                        // 获取子节点
                        $children = \app\admin\model\Ocation::getChildOcationIds($oid, false);
                        $filter_ids = array_merge($filter_ids, $children);
                        // 添加当前节点
                        $filter_ids[] = $oid;
                    }
                }
                // 确保所有ID都是整数，并去重
                $filter_ids = array_unique(array_map('intval', $filter_ids));
            } else {
                // 如果找不到ocation_id，则返回空数组
                $filter_ids = [];
            }
        }
        if ($_SESSION['think']['admin']['group_id'] != 1) {
            $where .= " and city_id='{$_SESSION['think']['admin']['city_id']}'";
        }
        if (isset($_REQUEST['addresslv_id']) && $_REQUEST['addresslv_id'] != "" && ($_SESSION['think']['admin']['group_id'] == "3" || $_SESSION['think']['admin']['group_id'] == "2")) {
            $where .= " and address_lv='{$_REQUEST['addresslv_id']}'";
        }
        if (isset($_REQUEST['addresslv_id']) && $_REQUEST['addresslv_id'] == "") {
            $where .= " and address_lv='{$_REQUEST['addresslv_id']}'";
        }
        if ($_SESSION['think']['admin']['group_id'] == 2 || $_SESSION['think']['admin']['group_id'] == 8) {
            $where .= " and city_id='{$_SESSION['think']['admin']['city_id']}'";
        }
        $relId = $this->request->get("rel_id") ? $this->request->get("rel_id") : $_SESSION['think']['admin']['rel_id'];
        $relType = $this->request->get("rel_type") ? $this->request->get("rel_type") : $_SESSION['think']['admin']['rel_type'];

        $page = 1;
        $pagesize = 50;
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                $keyField = $this->request->request("keyField");
                $keyValue = $this->request->request("keyValue");
                $page = $this->request->request("pageNumber");
                $page = ($page == null) ? 1 : $page;
                $pagesize = $this->request->request("pageSize");
                $pagesize = ($pagesize == null) ? 10 : $pagesize;
                if ($keyValue != "") {
                    $nn = preg_split("/\,/", $keyValue);
                    for ($i = 0; $i < count($nn); $i++) {
                        $where .= " and $keyField='{$nn[$i]}'";
                    }
                }
            }
        }
        // 获取所有数据用于构建树形结构
        $sql = "select id,pid,name,address,address_lv_octionlevel,status,weigh from fa_ocation $where order by weigh desc,id asc";
        $list = db()->query($sql);

        // 递归查找并补全所有祖先节点，确保根节点一定在$list中,然后找出$filter_ids的最近的上级父节点
        $idToPid = [];
        $allIds = array_column($list, 'id');
        foreach ($list as $item) {
            $idToPid[$item['id']] = $item['pid'];
        }
        if (!function_exists('fetchAncestors')) {
            function fetchAncestors($id, &$idToPid, &$allIds, &$list) {
                if (!isset($idToPid[$id])) {
                    $node = Db::name('ocation')->where('id', $id)->find();
                    if ($node) {
                        $idToPid[$node['id']] = $node['pid'];
                        $allIds[] = $node['id'];
                        $list[] = $node;
                        if ($node['pid']) {
                            fetchAncestors($node['pid'], $idToPid, $allIds, $list);
                        }
                    }
                }
            }
        }
        foreach ($list as $item) {
            fetchAncestors($item['id'], $idToPid, $allIds, $list);
        }
        $allIds = array_unique($allIds);
        $list = array_values(array_column($list, null, 'id'));
        // 找到最近的有效父节点作为根节点
        function findNearestCommonParent($ids, $idToPid) {
            if (empty($ids)) {
                return null;
            }

            // 构建每个节点到根的路径
            $paths = [];
            foreach ($ids as $id) {
                $path = [];
                $currentId = $id;
                while (isset($idToPid[$currentId]) && $idToPid[$currentId]) {
                    $path[] = $currentId;
                    $currentId = $idToPid[$currentId];
                }
                $path[] = $currentId; // 添加根节点
                $paths[$id] = array_reverse($path);
            }

            // 如果只有一个节点，直接返回其父节点
            if (count($paths) === 1) {
                $path = reset($paths);
                return count($path) > 1 ? $path[count($path) - 2] : $path[0];
            }

            // 找到所有路径中最短的长度
            $minLen = min(array_map('count', $paths));

            // 从最后一个共同祖先向下查找，直到找到第一个分叉点的父节点
            $lastCommon = null;
            for ($i = 0; $i < $minLen; $i++) {
                $firstPath = reset($paths);
                $val = $firstPath[$i];
                $isCommon = true;

                foreach ($paths as $path) {
                    if ($path[$i] !== $val) {
                        $isCommon = false;
                        break;
                    }
                }

                if (!$isCommon) {
                    // 返回最后一个共同节点
                    return $lastCommon;
                }
                $lastCommon = $val;
            }

            // 如果所有节点都在同一条路径上，返回最深的共同节点
            return $lastCommon;
        }

        $rootId = findNearestCommonParent($filter_ids, $idToPid);
        // 使用Tree类构建树形结构
        $tree = Tree::instance();
        $tree->init($list, 'pid');
        // 获取父节点信息
        $parentNodes = [];
        if ($rootId) {
            // 获取所有父级节点
            $parentNodes = $tree->getParents($rootId, false);
            // 如果需要包含父教学点信息，获取完整树结构
            if ($rootId > 0) {
                // 方案3：获取父节点及其所有子节点
                $currentNode = Db::name('ocation')->where('id', $rootId)->find();
                if ($currentNode && $currentNode['pid'] > 0) {
                    // 如果有父节点，以父节点为根获取树结构
                    $treeList = $tree->getTreeList($tree->getTreeArray($currentNode['pid']), 'name');
                } else {
                    // 如果没有父节点，以当前节点为根
                    $treeList = $tree->getTreeList($tree->getTreeArray($rootId), 'name');
                }
            } else {
                // rootId为0时，获取完整树结构
                $treeList = $tree->getTreeList($tree->getTreeArray(0), 'name');
            }

            // // 为父级节点添加特殊标记，以便在前端展示时区分
            // foreach ($parentNodes as &$parent) {
            //     // 为父节点添加层级信息和特殊标记
            //     $parent['spacer'] = '';
            //     $parent['name'] = $parent['name'] . ' (上级)';
            //     $parent['is_parent'] = true; // 添加标记表示这是父节点
            // }

            // 将父节点添加到树列表的前面
            //$treeList = array_merge($parentNodes, $treeList);
        } else {
            $treeList = [];
        }


        // 为每个节点添加层级信息
        foreach ($treeList as $key => &$item) {
            // 计算层级深度
            $level = substr_count($item['name'], '&nbsp;') / 4; // 每级4个&nbsp;
            $item['level'] = $level;

            // 清理name中的HTML标签，保留纯文本
            $item['name'] = strip_tags(str_replace('&nbsp;', '', $item['name']));

            // 添加parent字段
            $item['parent'] = $item['pid'] == 0 ? '#' : $item['pid'];

            // 计算是否有子节点
            $childCount = 0;
            foreach ($list as $child) {
                if ($child['pid'] == $item['id']) {
                    $childCount++;
                }
            }
            $item['haschild'] = $childCount;

            // 添加其他必要字段
            $item['type'] = $childCount > 0 ? 'folder' : 'file';
            $item['status'] = $item['status'] == 1 ? 'normal' : 'hidden';
        }

        // 分页处理
        $total = count($treeList);
        if ($this->request->isAjax() && isset($page) && isset($pagesize)) {
            $treeList = array_slice($treeList, ($page - 1) * $pagesize, $pagesize);
        }

        // 检查是否是Bootstrap Table请求（通过参数判断）
        if ($this->request->isAjax() && ($this->request->has('offset') || $this->request->has('limit'))) {
            // Bootstrap Table格式
            // 修复：确保返回的是数组而不是对象
            echo json_encode(array("total" => $total, "rows" => array_values($treeList)), JSON_UNESCAPED_UNICODE);
        } else {
            // 下拉框格式（保持兼容性）
            // 修复：确保返回的是数组而不是对象
            echo json_encode(array("list" => array_values($treeList), "total" => $total), JSON_UNESCAPED_UNICODE);
        }
        exit;
    }